<%--
  Created by IntelliJ IDEA.
  User: zmjha
  Date: 2020/6/20
  Time: 16:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@taglib  prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%>

<html>
<head>
    <title>主页面</title>
</head>
<body>
<button type="button" onclick="add()">添加</button>

<table border="1">
    <tr>
        <th>书名</th>
        <th>价格</th>
        <th>作者</th>
        <th>操作</th>
    </tr>

    <c:forEach items="${bookList }" var="book" varStatus="status">
        <c:if test="${status.count%2==0}">
            <tr>
                <td style="color: green">${book.bookName}</td>
                <td style="color: green">${book.price}</td>
                <td style="color: green">${book.author}</td>

                <td>
                    <button type="button" onclick="update(${book.id})">更改</button>
                    <button type="button" onclick="del(${book.id})">删除</button>
                </td>
            </tr>
        </c:if>
        <c:if test="${status.count%2!=0}">
            <tr>
                <td style="color: red">${book.bookName}</td>
                <td style="color: red">${book.price}</td>
                <td style="color: red">${book.author}</td>

                <td>
                    <button type="button" onclick="update(${book.id})">更改</button>
                    <button type="button" onclick="del(${book.id})">删除</button>
                </td>
            </tr>
        </c:if>


    </c:forEach>
    <hr>
    总共${pageInfo.total}记录，总共${pageInfo.pages}页
    <c:if test="${pageInfo.hasPreviousPage==true}">
        <a href="tomain?pageNum=${pageInfo.prePage}">上一页</a>
    </c:if>
    <c:if test="${pageInfo.hasPreviousPage==false}">
        <a href="tomain?pageNum=${pageInfo.navigateLastPage}">上一页</a>
    </c:if>

     <a href="tomain?pageNum=1">第一页</a><a href="tomain?pageNum=2">第二页</a><a href="tomain?pageNum=3">第三页</a>


    <c:if test="${pageInfo.hasNextPage==true}">
        <a href="tomain?pageNum=${pageInfo.nextPage}">下一页</a>
    </c:if>
    <c:if test="${pageInfo.hasNextPage==false}">
        <a href="tomain?pageNum=${pageInfo.navigateFirstPage}">下一页</a>
    </c:if>

</table>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
    function add() {
        location.href="toUpdate"
    }
    function update(sid) {
        var id=0;
        if(sid!=undefined){
            id=sid;
            location.href="toUpdate?id="+id;
        }
    }
    function del(sid) {
        var id=0;
        if(sid!=undefined) {
            id =sid;
            $.ajax({
                url:"del",
                type:"post",
                data:{"id":id},
                dataType:"json",
                success:function(data){
                    if(data.code==1){
                        alert(data.mes);
                        location.reload()
                    }else {
                        alert(data.mes)

                    }
                }
            })
        }
    }
</script>
</html>
